<html>
<head>
  <title>create_array.h</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<pre>
<font color="0000ff"><strong>#include <font color="#008000">&lt;Python.h&gt;</font></strong></font>
<font color="0000ff"><strong>#include <font color="#008000">&lt;Numeric/arrayobject.h&gt;</font></strong></font>

<font color="0000ff"><strong>#include <font color="#008000">&lt;sys/types.h&gt;</font></strong></font>
<font color="0000ff"><strong>#include <font color="#008000">&lt;stdio.h&gt;</font></strong></font>
<font color="0000ff"><strong>#include <font color="#008000">&lt;stdlib.h&gt;</font></strong></font>

<font color="#444444">/* This function takes 1 argument, N, and creates a 1D array of that size 
   and returns it to python with it's values initialized with 0 to N-1. */</font>
<strong>static</strong> <font color="#2040a0">PyObject</font> <font color="4444FF">*</font><font color="#2040a0">create_array</font><font color="4444FF">(</font><font color="#2040a0">PyObject</font> <font color="4444FF">*</font> <font color="#2040a0">self</font>, <font color="#2040a0">PyObject</font> <font color="4444FF">*</font> <font color="#2040a0">args</font><font color="4444FF">)</font>
<font color="4444FF"><strong>{</strong></font>
    <strong>int</strong> <font color="#2040a0">N</font>, <font color="#2040a0">i</font><font color="4444FF">;</font>
    <font color="#2040a0">PyArrayObject</font> <font color="4444FF">*</font><font color="#2040a0">result</font><font color="4444FF">;</font>
    <strong>int</strong> <font color="#2040a0">dimensions</font><font color="4444FF">[</font><font color="#FF0000">1</font><font color="4444FF">]</font><font color="4444FF">;</font>
    <strong>int</strong> <font color="4444FF">*</font><font color="#2040a0">buffer</font><font color="4444FF">;</font>

    <strong>if</strong> <font color="4444FF">(</font><font color="4444FF">!</font><font color="#2040a0">PyArg_ParseTuple</font><font color="4444FF">(</font><font color="#2040a0">args</font>, <font color="#008000">&quot;i&quot;</font>, <font color="4444FF">&amp;</font><font color="#2040a0">N</font><font color="4444FF">)</font><font color="4444FF">)</font>
    <font color="4444FF"><strong>{</strong></font>
      <strong>return</strong> <font color="#2040a0">NULL</font><font color="4444FF">;</font>
    <font color="4444FF"><strong>}</strong></font>
    <font color="#2040a0">dimensions</font><font color="4444FF">[</font><font color="#FF0000">0</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#2040a0">N</font><font color="4444FF">;</font>
    <font color="#2040a0">result</font> <font color="4444FF">=</font> <font color="4444FF">(</font><font color="#2040a0">PyArrayObject</font> <font color="4444FF">*</font><font color="4444FF">)</font> <font color="#2040a0">PyArray_FromDims</font><font color="4444FF">(</font><font color="#FF0000">1</font>, <font color="#2040a0">dimensions</font>, <font color="#2040a0">PyArray_INT</font><font color="4444FF">)</font><font color="4444FF">;</font>
    
    <font color="#2040a0">buffer</font> <font color="4444FF">=</font> <font color="#2040a0">result</font><font color="4444FF">-</font><font color="4444FF">&gt;</font><font color="#2040a0">data</font><font color="4444FF">;</font>
    <strong>for</strong><font color="4444FF">(</font> <font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">;</font> <font color="#2040a0">i</font> <font color="4444FF">&lt;</font> <font color="#2040a0">N</font><font color="4444FF">;</font> <font color="#2040a0">i</font><font color="4444FF">+</font><font color="4444FF">+</font> <font color="4444FF">)</font> 
    <font color="4444FF"><strong>{</strong></font>
      <font color="#2040a0">buffer</font><font color="4444FF">[</font><font color="#2040a0">i</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#2040a0">i</font><font color="4444FF">;</font> 
    <font color="4444FF"><strong>}</strong></font>

    <strong>return</strong> <font color="#2040a0">PyArray_Return</font><font color="4444FF">(</font><font color="#2040a0">result</font><font color="4444FF">)</font><font color="4444FF">;</font>
<font color="4444FF"><strong>}</strong></font>

</pre>
<hr>
syntax highlighted by <a href="http://www.palfrader.org/code2html">Code2HTML</a>, v. 0.9.1
</body>
</html>
